<?php

namespace App\Models\Orm;


use App\Models\AbsBaseModel;


/**
 *
 * Area
 */
class User extends AbsBaseModel
{


    public $timestamps = false;
    public $incrementing = false;

    /**
     *
     *  设置表名
     */
    const TABLE_NAME = 'user';
    const PRIMARY_KEY = 'id';

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = self::TABLE_NAME;
    //主键id
    protected $primaryKey = self::PRIMARY_KEY;
    //查询字段
    protected $visible = [];
    //加黑名单
    protected $guarded = [];

    /**
     * 获取用户资金统计
     *
     * @return mixed
     */
    public function getAccount()
    {
        return $this->hasOne(AwUserAccount::class, 'user_id', 'id')->getResults();
    }

    /**
     * 获取用户阿里账户
     *
     * @return mixed
     */
    public function getAliUser()
    {
        return $this->hasOne(AliUser::class, 'uid', 'id')->getResults();
    }

    /**
     * 获取用户积分统计
     * @return mixed
     */
    public function getIntegral()
    {
        return $this->hasOne(AwUserIntegral::class, 'user_id', 'id')->getResults();
    }


    /**
     * 获取用户资金记录最近10条,
     * 根据动作: action 和动作记录ID: order_id 去重,
     * 同一个用户动作记录只查询一条
     *
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function getAccountLogs($page = 1)
    {
        // 默认第一页,取最新十条。如果传了页码,根据页码取10条。保证传值不为负数
        $offset = max(0, (10 * (floor($page) - 1)));

        return $this->hasMany(AwUserAccountLog::class, 'user_id', 'id')
            ->getQuery()
            ->groupBy(['action', 'order_id'])
            ->orderBy('created_at', 'desc')
            ->offset($offset)
            ->limit(10)
            ->get();
    }


}
